<?php
class Content_model extends CI_Model
{	
	function __construct()
	{
		parent::__construct();
		
	}
	private function _required($required, $data)
	{
		foreach($required as $field) if(!isset($data[$field])) return false;
		return true;
	}
	
	private function _default($defaults, $options)
	{
		return array_merge($defaults, $options);
	}
	
	function insert($options = array())
	{
		//if(!$this->_required(array(.$qualification_fields.), $options)) 
		//		return false;
	
		//$options = $this->_default(array('u_created_on' => sql_current_datetime()), $options);
		$qualificationArray = array('content_id', 'language', 'page_id', 'page_content', 'seo_keywords', 'seo_description');
		foreach($qualificationArray as $qualifier)
			if(isset($options[$qualifier])) 
				$this->db->set($qualifier, $options[$qualifier]);
	 
		$this->db->insert('pages_content');
	
		return $this->db->insert_id();
	}
	
	function update($options = array())
	{
		if(!$this->_required(array('content_id'), $options)) 
			return false;

		$qualificationArray = array('content_id', 'language', 'page_id', 'page_content', 'seo_keywords', 'seo_description');
		foreach($qualificationArray as $qualifier)
			if(isset($options[$qualifier])) 
				$this->db->set($qualifier, $options[$qualifier]);
	
		$this->db->where('content_id', $options [ 'content_id' ]);	
		$this->db->update('pages_content');	
		return $this->db->affected_rows();
	}
	
	function get($options = array())
	{
		$options = $this->_default(array('sort_direction' => 'asc'), $options);
	
		$qualificationArray = array('content_id', 'language', 'page_id', 'page_content', 'seo_keywords', 'seo_description');
		foreach($qualificationArray as $qualifier)
			if(isset($options[$qualifier])) 
				$this->db->where($qualifier, $options[$qualifier]);
	
		if(isset($options ['limit']) && isset($options ['offset'])) 
			$this->db->limit($options ['limit'], $options ['offset']);
		else if(isset($options ['limit'])) 
			$this->db->limit($options ['limit']);
	
		if(isset($options ['sort_by'])) 
		$this->db->order_by($options ['sort_by'], $options ['sort_direction']);
		
		$query = $this->db->get('pages_content');
		
		if($query->num_rows() == 0) 
			return false;
	
		if(isset($options ['content_id']))
			return $query->row_array(0);
		else
			return $query->result_array();
	}
	
	function delete($options = array())
	{
		if(!$this->_required(array('content_id'), $options)) 
			return false;
	
		$this->db->where('content_id', $options ['content_id']);
		$this->db->delete('pages_content');
	}

}